/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Thomas
 */
import java.util.ArrayList;
import cs1.Keyboard;
//Basic Combat between 2 targets. 
//Add functionality for 2 parties. 

public class combat {
    //Adding Support for multiple combatObjects. Intended party size is 4.
    ArrayList<combatObject> teamA, teamB;
    //one on one DEPRECATED
    //combatObject A, B;
    //Change turnOrder into 2 lists
    ArrayList<combatObject> turnOrderA;
    ArrayList<combatObject> turnOrderB;
    
    int turn;
    //Intended to sort the List of Combatants by speed. Selection sort for ease. 
    private void sortList(ArrayList<combatObject> tar){
        combatObject temp, cur;
        int index;
        
        for(int i = 0; i < tar.size(); i++){
            cur = tar.get(i);
            index = i;
            for(int j = i + 1; j < tar.size(); j++){
                if(tar.get(j).getSpeed() > cur.getSpeed()){
                    cur = tar.get(j);
                    index = j;
                }
            }
            temp = tar.get(i);
            tar.set(i, cur);
            tar.set(index, temp);
        }
    }
    public combat(ArrayList<combatObject> A, ArrayList<combatObject> B){
        teamA = A;
        teamB = B;
        turnOrderA = new ArrayList<combatObject>(teamA.size());
        turnOrderB = new ArrayList<combatObject>(teamB.size());
        turnOrderA.addAll(teamA);
        turnOrderB.addAll(teamB);
        sortList(turnOrderA);
        sortList(turnOrderB);
        for(int i = 0;  i < turnOrderA.size(); i++){
            System.out.println(turnOrderA.get(i).getName() + " " + turnOrderA.get(i).getSpeed() +"\n");
        }
        for(int i = 0;  i < turnOrderB.size(); i++){
            System.out.println(turnOrderB.get(i).getName() + " " + turnOrderB.get(i).getSpeed() +"\n");
        }
    }
    
//    public combat(combatObject a, combatObject b){
//        A = a;
//        B = b;
//        if (A.getSpeed() > B.getSpeed()){
//            turnOrder.add(A);
//            turnOrder.add(B);
//        }
//        else{
//            turnOrder.add(B);
//            turnOrder.add(A);
//        }
//    }
    //Debug Deprecated. 
//    void declareAttack(combatObject a, combatObject b){
//        System.out.println(a.getName() + " attacks " + b.getName() + " for " +
//                a.getAttack() + "damage.\n");
//        b.HP -= a.getAttack();
//    }
    public void battle(){
        int a, b, limita = teamA.size(), limitb = teamB.size();
        declareFightStart();
        for(turn = 1; checkHP(); turn++){
            declareRound();
            declareCombatantHP();
            a = 0;
            b = 0;
            
            for(int i = 0; i < (turnOrderA.size() + turnOrderB.size()); i++){
                if(a < limita && b < limitb && (turnOrderA.get(a).getSpeed() > turnOrderB.get(b).getSpeed())){
                    //A can attack B. Insert Attackcode here. 
                    System.out.println("Friendly Phase: " + turnOrderA.get(a).getName());
                    turnOrderA.get(a).act(turnOrderA, turnOrderB);
                    a++;
                }else if(a < limita && b < limitb && turnOrderB.get(b).getSpeed() > turnOrderA.get(a).getSpeed()){
                    System.out.println("Enemy Phase: " + turnOrderB.get(b).getName());
                    turnOrderB.get(b).act(turnOrderB, turnOrderA);
                    b++;
                }else if(b < limitb){
                    //If for the lowest.
                    System.out.println("Enemy Phase: " + turnOrderB.get(b).getName());
                    turnOrderB.get(b).act(turnOrderB, turnOrderA);
                    b++;
                    //;
                }else if(a < limita){
                    System.out.println("Friendly Phase: " + turnOrderA.get(a).getName());
                    turnOrderA.get(a).act(turnOrderA, turnOrderB);
                    a++;
                }else{
                    break;
                }
            }
//            for(int i = 0; i<turnOrder.size(); i++){
//                if (i == turnOrder.size() - 1)
//                    declareAttack(turnOrder.get(i), turnOrder.get(0));
//                else
//                    declareAttack(turnOrder.get(i), turnOrder.get(i + 1));
//            }
        }
        declareWinner();
    }
    void declareFightStart(){
        System.out.println("Battle Begin!");
    }
    void declareRound(){
        System.out.println("Round " + turn + ".");
    }
    void declareCombatantHP(){
        System.out.println("Party Stats:");
        for(int i = 0; i < teamA.size(); i++){
            System.out.println((i + 1) +".) " + teamA.get(i).getName() + " " + teamA.get(i).HPString());
        }
        System.out.println("Enemy Stats:");
        for(int i = 0; i < teamB.size(); i++){
            System.out.println((i + 1) +".) " + teamB.get(i).getName() + " " + teamB.get(i).HPString());
        }
        
    }
    boolean checkTeamHP(ArrayList<combatObject> tar){
        boolean ans = false;
        for(int i = 0; i < tar.size(); i++){
            if(tar.get(i).getHP() > 0){
                ans = true;
            }
        }
        return ans;
    }
    boolean checkHP(){
        if (!checkTeamHP(teamA))
            return false;
        else if(!checkTeamHP(teamB))
            return false;
        else
            return true;
    }
    void declareWinner(){
        String temp;
        if(checkTeamHP(teamA))
            temp = "A";
        else
            temp = "B";
        System.out.println("Battle Complete! Victory goes to Team " + temp+ "!\n");
    }
    public static void main(String[]args){
        playerObject po = new playerObject();
        //po.addSkill(new ability("Death Ray", "Kills everything."));
        po.addSkill(new attack(false));
        po.addSkill(new attack(true));
        po.addSkill(new abilityHeal());
        combatObject ap = new combatObject("John", 1000, 25, 25);
        combatObject bp = new combatObject("Jane", 1024, 24, 30);
        combatObject cp = new combatObject("Kennedy", 1500, 50, 19);
        ArrayList<combatObject> party = new ArrayList<combatObject>(2);
        party.add(ap);
        party.add(bp);
        ArrayList<combatObject> playerParty = new ArrayList<combatObject>(2);
        playerParty.add(cp);
        playerParty.add((combatObject)po);
        combat mainApp = new combat(playerParty, party);
        mainApp.battle();
    }
}